<?php
/**
 * Created by PhpStorm.
 * User: zhangjian37
 * Date: 2016/9/16
 * Time: 9:55
 */

$RootDir = $_SERVER['DOCUMENT_ROOT'];
include $RootDir . '/utils/mysql/MysqlUtils.php';
include $RootDir . '/utils/CommonConstant.php';
include $RootDir . '/utils/FileUtils.php';
include $RootDir . '/xz/XZ.php';
include $RootDir . 'MainRecord.php';

//接受 版本信息
$url = $_POST['url'];
$version = $_POST['version'];
$bus = $_POST['bus'];
$train = $_POST['train'];
$gg = $_POST['gg'];
$help_type = $_POST['help_type'];
$help_publish_type = $_POST['help_publish_type'];
$check_status = $_POST['check_status'];
$about = $_POST['about'];
$user_protocol = $_POST['user_protocol'];
$password_protect = $_POST['password_protect'];

//接受 app启动信息
$phone_id = $_POST['phone_id'];
$user_id = $_POST['user_id'];
$open_time = $_POST['open_time'];
$phone_manu = $_POST['phone_manu'];
$phone_type = $_POST['phone_type'];
$os_type = $_POST['os_type'];
$os_version = $_POST['os_version'];
$ip = $_POST['ip'];
$longitude = $_POST['longitude'];
$latitude = $_POST['latitude'];

//首先保存app启动信息 到表 phone_info open_info中去
$conn = conn_mysql();

///////////////////////////插入open_info中去
$query = "insert into open_info(phone_id,user_id,open_time,phone_manu,phone_type,os_type,os_version,ip,longitude,latitud)
          values('$phone_id',$user_id,'$open_time','$phone_manu','$phone_type','$os_type','$os_version','$ip',$longitude,$latitude);";
if (!mysql_query($query,$conn)) {
    die('err: ' . mysql_error());
}

//////////////////////////判定phone_info是否有此phone_id的数据，如果有就更新，如果没有就插入
$query = "select count(1) as cout from phone_info WHERE phone_id = '$phone_id'";
$result = queryRecord($query,$conn);
//////////////////////////echo mysql_num_rows($result);
while ($row=mysql_fetch_array($result, MYSQL_ASSOC)) {
    $cout = $row['cout'];
}
if ($cout == 1 ){
    $query = "update phone_info set user_id = $user_id,open_time = '$open_time',phone_manu = '$phone_manu',phone_type = '$phone_type',
              os_type = '$os_type',os_version = '$os_version',ip = '$ip',longitude = $longitude,latitud = $latitude);";
}else{
    $query = "insert into phone_info(phone_id,user_id,open_time,phone_manu,phone_type,os_type,os_version,ip,longitude,latitud)
          values('$phone_id',$user_id,'$open_time','$phone_manu','$phone_type','$os_type','$os_version','$ip',$longitude,$latitude);";
}

if (!mysql_query($query,$conn)) {
    die('err: ' . mysql_error());
}

//然后匹配版本信息，将对应版本有更新的，进行数据提取和返回
$query = "select bus,train,gg,help_type,help_publish_type,check_status,application_status,about,user_protocol,password_protect from dict_version WHERE id = $version";
$result = queryRecord($query,$conn);
//echo mysql_num_rows($result);
while ($row=mysql_fetch_array($result, MYSQL_ASSOC)) {
    $dv_train = $row['train'];
    $dv_bus = $row['bus'];
    $dv_gg = $row['gg'];
    $dv_help_type = $row['help_type'];
    $dv_help_publish_type = $row['help_publish_type'];
    $dv_check_status = $row['check_status'];
    $dv_application_status = $row['application_status'];
    $dv_about = $row['about'];
    $dv_user_protocol = $row['user_protocol'];
    $password_protect = $row['password_protect'];
    break;
}

$mainRecord = new MainRecord();

if ($gg < $dv_gg){
    $mainRecord->xzRecord = getGGS($url);
}
